﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using UFIDA.U9.AR.Enums;
using UFIDA.U9.AR.Receival;
using UFIDA.U9.ISV.PUB.CustZLAPI.Model;
using UFSoft.UBF.Business;
using UFSoft.UBF.MVC;
using UFSoft.UBF.MVC.Common;
using UFSoft.UBF.Util.Log;

namespace UFIDA.U9.ISV.PUB.CustZLAPI
{
    /// <summary>
    /// 收款单标准款明细
    /// </summary>
    public class CustRecMonryDetialController : OpenAPIController
    {
        /// <summary>
        /// 添加标准款明细 信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public ApiResult<ResDataModel> AddDetial([FromBody] PramModel param)
        {
            ILogger logger = LoggerManager.GetLogger("RecMonryDetialController");
            logger.Error(string.Format("RecMonryDetialController：{0}", JsonConvert.SerializeObject(param)), new object[0]);
            string orgid = UFSoft.UBF.Util.Context.PlatformContext.Current.OrgID;
            UFIDA.U9.AR.Receival.RecBillHead e = UFIDA.U9.AR.Receival.RecBillHead.Finder.Find(" DOCNO='" + param.Docno + "' and Org=" + orgid + "");
            if (e == null)
            {
                return new ApiResult<ResDataModel>(null, -1, "未找到收款单！");
            }
            try
            {
                using (ISession session = Session.Open())
                {
                    foreach (Detial item in param.detial)
                    {
                        RecDetail r = RecDetail.Create(e);
                        r.RecDetailSrcType = RecDetailSrcTypeEnum.GetFromValue(item.RecDetailSrcType);
                        r.DocNo = item.DocNo;
                        r.SrcBillID = item.SrcBillID;
                        r.TaxCtrlInvoiceNum = item.TaxCtrlInvoiceNum;
                        r.DocDate = item.DocDate;
                        r.ReceiveInvoiceDate = item.ReceiveInvoiceDate;
                        r.TotalMoney = item.TotalMoney;
                    }
                    if (param.detial.Count > 0)
                    {
                        session.Commit();
                    }
                }
                return new ApiResult<ResDataModel>(null,0,"完成");
            }
            catch (Exception ex)
            {
                logger.Error(string.Format("RecMonryDetialController错误：{0}\r\n {1}", ex.Message, ex.StackTrace));
                return new ApiResult<ResDataModel>(null, -1, ex.Message);
            }
        }


        #region PramAPIModel
        /// <summary>
        /// 请求参数
        /// </summary>
        public class PramModel
        {
            /// <summary>
            /// 收款单 单号
            /// </summary>
            public string Docno { get; set; }
            /// <summary>
            /// 标准款明细
            /// </summary>
            public List<Detial> detial = new List<Detial>();
        }
        /// <summary>
        /// 标准明细行
        /// </summary>
        public class Detial
        {
            public int SrcBillDirect { get; set; }
            public long SrcBillID { get; set; }
            public decimal TotalMoney { get; set; }
            public DateTime ReceiveInvoiceDate { get; set; }
            public DateTime DocDate { get; set; }
            public string TaxCtrlInvoiceNum { get; set; }
            /// <summary>
            /// 来源单号
            /// </summary>
            public string DocNo { get; set; }

            public int RecDetailSrcType { get; set; }
        }

        /// <summary>
        /// 返回类
        /// </summary>
        public class ResDataModel
        {
            /// <summary>
            /// 返回信息
            /// </summary>
            public string str { get; set; }
        }


        #endregion

    }
}
